package xyz.chaobei.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import xyz.chaobei.mapper.TbAdminMapper;
import xyz.chaobei.model.TbAdminModel;
import xyz.chaobei.service.pojo.TbAdminPageAO;
import xyz.chaobei.service.pojo.TbAdminSaveAO;
import xyz.chaobei.service.TbAdminService;

import java.util.List;
import java.util.Objects;

/**
 * 用户管理 Service impl
 *
 * @author mrc
 * @since 2021-01-07 21:05:04
 */
@Service
public class TbAdminServiceimpl implements TbAdminService {

    @Autowired
    private TbAdminMapper tbAdminMapper;

    @Override
    public List<TbAdminModel> findAll() {
        return tbAdminMapper.selectList(null);
    }

    @Override
    public TbAdminModel findOne(Integer id,boolean isExc) {

        TbAdminModel result = tbAdminMapper.selectById(id);

        if (Objects.isNull(result) && isExc) {
            throw new RuntimeException("未查询到相关信息");
        }
        return result;
    }

    @Override
    public Page<TbAdminModel> findPage(TbAdminPageAO pageAO) {

        Page page = new Page(pageAO.getCurrent(),pageAO.getSize());
        QueryWrapper wrapper = new QueryWrapper();

        wrapper.eq("`username`", pageAO.getUsername());
        wrapper.eq("`lock`", pageAO.getLock());
        wrapper.eq("`nick_name`", pageAO.getNickName());

        tbAdminMapper.selectPage(page, wrapper);

        return page;
    }

    @Override
    public boolean save(TbAdminSaveAO params) {

        TbAdminModel model = new TbAdminModel();
        BeanUtils.copyProperties(params,model);
        /**
         * 你的逻辑写在这里
         */
        int num = tbAdminMapper.insert(model);

        return SqlHelper.retBool(num);
    }

    @Override
    public boolean updateById(TbAdminSaveAO params, Integer id) {

        TbAdminModel model = new TbAdminModel();
        BeanUtils.copyProperties(params,model);

        /**
         * 你的逻辑写在这里
         */
        model.setId(id);
        int num = tbAdminMapper.updateById(model);

        return SqlHelper.retBool(num);
    }

    @Override
    public boolean deleteById(Integer id) {

        /**
         * 你的逻辑写在这里
         */
        int num = tbAdminMapper.deleteById(id);
        return SqlHelper.retBool(num);
    }

}
